/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */
package com.excesys.exsecs.om.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.excesys.exsecs.om.entity.OmRepairPlan;
import com.excesys.exsecs.om.mapper.OmRepairPlanMapper;
import com.excesys.exsecs.om.service.OmRepairPlanService;
import com.excesys.exsecs.om.utils.PlanToSheetUtils;
import com.excesys.exsecs.om.vo.RepairPlanExcelVo;
import com.excesys.exsecs.om.vo.RepairPlanVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class OmRepairPlanServiceImpl extends ServiceImpl<OmRepairPlanMapper, OmRepairPlan>
		implements OmRepairPlanService {

	private final OmRepairPlanMapper omRepairPlanMapper;

	private final PlanToSheetUtils planToSheetUtils;

	@Override
	public IPage<RepairPlanVo> getPlanWithRelPage(Page page, OmRepairPlan repairPlan) {
		return omRepairPlanMapper.getRepairPlanWithRelPage(page, repairPlan);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public Boolean saveRepairPlan(OmRepairPlan repairPlan) {
		baseMapper.insert(repairPlan);
		planToSheetUtils.generatorRepairTaskToday(repairPlan);
		return Boolean.TRUE;
	}

	@Override
	public List<OmRepairPlan> getList() {
		return omRepairPlanMapper.getList();
	}

	@Override
	public List<RepairPlanExcelVo> listBamSBuildings() {
		List<OmRepairPlan> bamSBuildings = this.list(Wrappers.emptyWrapper());
		// 转换成execl 对象输出
		return bamSBuildings.stream().map(buildings -> {
			RepairPlanExcelVo repairPlanExcelVo = new RepairPlanExcelVo();
			BeanUtil.copyProperties(buildings, repairPlanExcelVo);
			return repairPlanExcelVo;
		}).collect(Collectors.toList());
	}

	@Override
	public OmRepairPlan execute(RepairPlanVo repairPlanVo) {
		Boolean flag = planToSheetUtils.executeRepairTaskToday(repairPlanVo);
		if (flag)
			repairPlanVo.setIsExecute("1");
		else
			repairPlanVo.setIsExecute("0");
		OmRepairPlan omRepairPlan = new OmRepairPlan();
		BeanUtil.copyProperties(repairPlanVo, omRepairPlan);
		baseMapper.updateById(omRepairPlan);
		return repairPlanVo;
	}

}
